package com.lc.midi.script;


import com.lc.midi.keyEvent.KeyEvent;
import com.lc.midi.keyEvent.KeyEvents;
import com.lc.midi.keyEvent.MidiKey;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class LeidianScript implements IScript{
	private static final String HEAD = "{\n"+
			"\"operations\": [\n";
	private static final String TAIL= "],\n\"recordInfo\":{"+"\n"+
			"\"loopType\": 0,\n"+
			"\"loopTimes\": 1,\n"+
			"\"circleDuration\": %d"+",\n"+//整个脚本时长
			"\"loopInterval\": 0,\n"+
			"\"loopDuration\": 0,\n"+
			"\"accelerateTimes\": 1,\n"+
			"\"recordName\": \"%s" + "\",\n"+//脚本名称
			"\"createTime\": \"%s\",\n"+//时间
			"\"playOnBoot\": false,\n"+
			"\"rebootTiming\": 0\n"+
			"}\n}\n";
	private String getTail(int totalTime, String name, String createTime) {
		return String.format(TAIL, totalTime, name, createTime);
	}
	
	@Override
	public String createScript(KeyEvents keyEvents, Config config) {
		StringBuilder s = new StringBuilder(HEAD);
		boolean[] isNoteOn = new boolean[200];
		Arrays.fill(isNoteOn, true);
		KeyEvents events = new KeyEvents();
		for (int i = 0; i < keyEvents.getKeyEventList().size(); i++) {
			KeyEvent event = new KeyEvent();
			event.setKeyList(keyEvents.getKeyEventList().get(i).getKeyList());
			event.setTime(keyEvents.getKeyEventList().get(i).getTime() + 1);
			events.addKeyEvent(event);
		}
		for (int i = 0; i <keyEvents.getKeyEventList().size(); i++) {
			events.addKeyEvent(keyEvents.getKeyEventList().get(i));
		}
		for (int i = 0; i < events.getKeyEventList().size(); i++) {
			if (i != 0) {
				s.append(",");
			}
			s.append("\n").append(press(events.getKeyEventList().get(i), isNoteOn, config));
		}
		//String writeFilePath = config.getFileName();
		//File file = new File(writeFilePath);
		//String name = file.getName();
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
		Date date = new Date(System.currentTimeMillis());
		s.append(getTail(events.getKeyEventList().get(events.getKeyEventList().size() - 1).getTime(), config.getFileName(), formatter.format(date)));
		/*
		 * FileWriter fileWriter = new FileWriter(file); fileWriter.write(s);
		 * fileWriter.close();
		 */
		return s.toString();
	}
	
	private String press(KeyEvent event, boolean[] isNoteOn, Config config) {
		StringBuilder f = new StringBuilder("{\n" +
				"\"timing\":%d,\n" +//time 单位ms
				"\"operationId\": \"PutMultiTouch\",\n" +
				"\"points\": [");
		List<MidiKey> keys = event.getKeyList();
		for (int i = 0; i < keys.size(); i++) {
			if (i != 0) {
				f.append(",");
			}
			f.append("\n").append(pressPos(keys.get(i), isNoteOn, config));
		}
		f.append("]\n" + "}\n");
		return String.format(f.toString(), event.getTime());
	}
	private String pressPos(MidiKey key, boolean[] isNoteOn, Config config) {
		String f = "{\n"+
				"\"id\": %d,\n"+//id
				"\"x\": %d,\n"+//10*x
				"\"y\": %d,\n"+//10*y
				"\"state\": %d\n"+//1:noteOn 0:noteOff
				"}\n";
		String s = String.format(f, key.getKeyId(), 
				10 * IScript.getKeyPosX(key.getKeyId(), config),
				10 * IScript.getKeyPosY(key.getKeyId(), config),
				isNoteOn[key.getKeyId()]?1:0);
		isNoteOn[key.getKeyId()] = !isNoteOn[key.getKeyId()];
		return s;
	}
}
